package com.xjtu.se.homework.client.login.controller;

import com.xjtu.se.homework.client.login.dto.ClientSearchDto;
import com.xjtu.se.homework.client.login.dto.ClientUserDto;
import com.xjtu.se.homework.client.login.service.ClientLoginService;
import com.xjtu.se.homework.common.Result;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*将用户信息缓存在android本地
    1、用户输入手机号或者email，首先在数据库中搜索
           1、如果手机和&&和email都搜索不到，说明是第一次登录，第一次登录即注册，将该用户的手机号码或者email存回数据库，其它信息使用默认
           2、如果手机号||email有一个搜索到了，将用户信息返回到android，然后缓存到android
*/
@RestController
@RequestMapping("/login")
public class ClientLoginAPI {
    @Resource
    private ClientLoginService clientLoginService;


    @GetMapping("/login")
    public Result search(ClientSearchDto clientSearchDto){
        List<ClientUserDto> searchResult = clientLoginService.search(clientSearchDto);
        if(searchResult.isEmpty()){//是空，返回默认信息，并插入数据库
            //id生成方法
            String u_id=clientSearchDto.getPhone();// 获得通过uuid算法得到的不重复的字符串
            ClientUserDto tempUser =new ClientUserDto(clientSearchDto.getEmail(), clientSearchDto.getPhone(),u_id);
            searchResult.add(tempUser);
            clientLoginService.insertUserInfor(tempUser);
        }
        System.out.println(searchResult.get(0).toString());
        return Result.success("",searchResult);
    }


}
